package com.kexun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kexun.entity.DO.Permission;
import com.kexun.entity.DO.Role;
import com.kexun.mapper.db1.RoleMapper;
import com.kexun.service.PermissionService;
import com.kexun.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl  extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionService permissionService;

    @Override
    public List<Role> roleList() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<Role> roleList(String roleName, int page, int limit) {

        QueryWrapper<Role> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(roleName)) {
            wrapper.eq("role_name", roleName);
        }
        return roleMapper.selectPage(new Page<Role>(page, limit), wrapper);
    }

    @Override
    public Role findRoleById(int id) {
        return roleMapper.findByRoleId(id);
    }

    @Override
    public void updateRole(String ids, int roleId, String roleName) {
        String[] newIds = ids.split(",");
        HashSet<Integer> newIdSet = new HashSet<>();
        HashSet<Integer> oldIdSet = new HashSet<>();
        for (String newId : newIds) {
            newIdSet.add(Integer.valueOf(newId));
        }
        List<Permission> oldPermission = permissionService.findByRoleId(roleId);
        for (Permission permission : oldPermission) {
            oldIdSet.add(permission.getId());
        }
        HashSet<Integer> addSet = new HashSet<>();
        HashSet<Integer> delSet = new HashSet<>();
        for (int oldId : oldIdSet) {
            //如果新的的里面不存在之前有的就要删除
            if (!newIdSet.contains(oldId)) {
                delSet.add(oldId);
            }
        }
        for (int newId : newIdSet) {
            //如果老的的里面存在新的就要添加
            if (!oldIdSet.contains(newId)) {
                addSet.add(newId);
            }
        }
        for (Integer addId : addSet) {
            permissionService.addPermissionId(roleId, addId);
        }
        for (Integer delId : delSet) {
            permissionService.removeByroleId(roleId, delId);
        }
        Role role = findRoleById(roleId);
        role.setRoleName(roleName);
        updateById(role);
    }


    @Override
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void addRole(String roleName, String ids) {
        Role role = new Role();
        role.setRoleName(roleName);
        addRole(role);
//        int i = 1 / 0;
//        测试事务是否正常回滚
        String[] newIds = ids.split(",");
        HashSet<Integer> newIdSet = new HashSet<>();
        for (String newId : newIds) {
            newIdSet.add(Integer.valueOf(newId));
        }
        for (Integer addId : newIdSet) {
            permissionService.addPermissionId(role.getId(), addId);
        }
    }

    @Override
    public void delRole(int roleId) {
        //先删除角色
        roleMapper.deleteById(roleId);
//        int i = 1 / 0; 测试事务回滚
        //再删除角色拥有的权限
        permissionService.removeByroleId(roleId);
    }

}
